<?php
declare (strict_types=1);

namespace app\controller;

use app\model\Habit as HabitModel;
use app\model\HabitLog as HabitLogModel;
use think\facade\Request;
use think\facade\Validate;

class Habit
{
    public function createHabit()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'title' => 'require|max:255',
            'frequency' => 'require|in:daily,weekly,monthly',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 创建习惯
        $habit = new HabitModel();
        $habit->user_id = $data['user_id'];  // 从登录状态获取
        $habit->title = $data['title'];
        $habit->frequency = $data['frequency'];
        $habit->save();

        return json(['message' => '习惯创建成功', 'habit_id' => $habit->habit_id]);
    }

    public function logHabit()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'habit_id' => 'require|integer',
            'status' => 'require|in:completed,skipped',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 记录习惯完成情况
        $habitLog = new HabitLogModel();
        $habitLog->habit_id = $data['habit_id'];
        $habitLog->log_date = date('Y-m-d');
        $habitLog->status = $data['status'];
        $habitLog->save();

        return json(['message' => '习惯记录成功', 'log_id' => $habitLog->log_id]);
    }

    // 更新习惯
    public function update()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'habit_id' => 'require|integer',
            'name' => 'require|max:100',
            'description' => 'max:500',
            'frequency' => 'require|in:daily,weekly,monthly',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $habit = HabitModel::find($data['habit_id']);
        if (!$habit) {
            return json(['status' => 'error', 'message' => 'Habit not found']);
        }

        $habit->name = $data['name'];
        $habit->description = $data['description'] ?? $habit->description;
        $habit->frequency = $data['frequency'];
        $habit->save();

        return json(['status' => 'success', 'message' => 'Habit updated successfully']);
    }

    // 删除习惯
    public function delete()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'habit_id' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $habit = HabitModel::find($data['habit_id']);
        if (!$habit) {
            return json(['status' => 'error', 'message' => 'Habit not found']);
        }

        $habit->delete();

        return json(['status' => 'success', 'message' => 'Habit deleted successfully']);
    }
}
